Skip to content

fix(auth): accept the OAuth v2 openhands_auth session cookie - #568

Merged
tofarr merged 1 commit into
mainfrom
fix/auth-accept-openhands-auth-cookie
Oct 9, 2026
Merged

tofarr merged 1 commit into
mainfrom
fix/auth-accept-openhands-auth-cookie

Conversation

@tofarr

@tofarr tofarr commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Why

The OpenHands web UI is moving to a dual-cookie authentication scheme (OpenHands/enterprise#525, epic OHE-3293): new logins receive a small JWT cookie named openhands_auth, while existing sessions keep the legacy chunked keycloak_auth cookie until it naturally expires. Upstream reads both and prefers openhands_auth.

Automation authenticates by forwarding the caller's cookie to OpenHands GET /api/v1/users/me. It previously only recognized keycloak_auth, so any user who logs in after the migration would get a 401 from Automation even though their session is valid.

What changed

openhands/automation/auth.py:

  • Replaced SESSION_COOKIE_NAME = "keycloak_auth" with SESSION_COOKIE_NAMES = ("openhands_auth", "keycloak_auth") — the new cookie is checked first.
  • _extract_session_cookie now returns the (name, value) pair and checks both names, still reassembling chunked values (<name>, <name>_1, …).
  • _extract_credential builds the cookie credential as name=value.
  • _upstream_headers forwards that name=value pair verbatim, so the original cookie name reaches upstream.

Why the name must be preserved

The two cookies have completely different structures — keycloak_auth is a chunked signed JWS wrapping Keycloak access/refresh tokens, openhands_auth is a small single JWT cookie — and upstream routes them to different decoders. Automation never decodes the cookie; it treats the value as opaque and forwards it under its own name, which is the only way upstream can parse it correctly.

Tests

tests/test_auth.py:

  • valid openhands_auth cookie authenticates, and the name=value is forwarded unchanged;
  • openhands_auth wins when both cookies are present;
  • falls back to keycloak_auth when the new cookie is absent;
  • an integration test mirroring the existing cookie-through-API test.

Verification

  • tests/test_auth.py (non-Docker): 53 passed; tests/test_cors.py: 9 passed.
  • ruff check, ruff format --check, and pyright all clean.
  • The 4 errors in the full run are the pre-existing Docker/Postgres-backed integration tests, which need a Docker daemon unavailable in this environment.

This PR was created by an AI agent (OpenHands) on behalf of the user.

@tofarr can click here to continue refining the PR

The OpenHands web UI is migrating to a dual-cookie scheme: new logins
receive the small JWT openhands_auth cookie, while existing sessions keep
the legacy chunked keycloak_auth cookie until it expires. Automation
authenticates by forwarding the caller's cookie to OpenHands
GET /api/v1/users/me, so it must accept either name or users who log in
after the migration get a 401.

Accept both cookies, preferring openhands_auth. The cookie is forwarded
verbatim under its original name because the two structures differ and
only upstream parses them; Automation itself never decodes the value.
@github-actions github-actions Bot added the type: fix A bug fix label Oct 9, 2026
@tofarr
tofarr marked this pull request as ready for review October 9, 2026 17:38
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@tofarr
tofarr enabled auto-merge (squash) October 9, 2026 17:45

@hieptl hieptl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙏

@tofarr
tofarr merged commit fb3f3cb into main Oct 9, 2026
13 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.19.2 Shipped in 1.19.2 label Oct 9, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.19.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.19.2 Shipped in 1.19.2 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants